import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'

let reducer = (state, action) => {
    let NewState = JSON.parse(JSON.stringify(state))
    switch (action.type) {
        case "SET_LIST":
            NewState.initList = action.res
            return NewState
        // 添加到购物车
        case "ADD_ITEM":
            let flag = NewState.carList.some(item => item.id === action.item.id)
            if (flag) {
                let index = NewState.carList.findIndex(item => item.id === action.item.id)
                NewState.carList[index].count += 1
            } else {
                NewState.carList.push(action.item)
            }

            // 对数量进行计算
            NewState.allCount = NewState.carList.reduce((prve, next) => {
                // 如果是选中状态在相加
                if (next.flag) {
                    return prve + next.count
                } else {
                    return prve
                }
            }, 0)
            // 对总价进行计算
            NewState.allPrice = NewState.carList.reduce((prve, next) => {
                if (next.flag) {
                    return prve + next.count * next.price
                } else {
                    return prve
                }
            }, 0)
            return NewState
        // 减少购物车数量
        case "DEL_ITEM":
            let index = NewState.carList.findIndex(item => item.id === action.item.id)
            NewState.carList[index].count -= 1

            // 对数量进行计算
            NewState.allCount = NewState.carList.reduce((prve, next) => {
                // 如果是选中状态在相加
                if (next.flag) {
                    return prve + next.count
                } else {
                    return prve
                }
            }, 0)
            // 对总价进行计算
            NewState.allPrice = NewState.carList.reduce((prve, next) => {
                if (next.flag) {
                    return prve + next.count * next.price
                } else {
                    return prve
                }
            }, 0)

            return NewState
        case "REMOVE_ITEM":
            NewState.carList.splice(action.index, 1)

            // 对数量进行计算
            NewState.allCount = NewState.carList.reduce((prve, next) => {
                // 如果是选中状态在相加
                if (next.flag) {
                    return prve + next.count
                } else {
                    return prve
                }
            }, 0)
            // 对总价进行计算
            NewState.allPrice = NewState.carList.reduce((prve, next) => {
                if (next.flag) {
                    return prve + next.count * next.price
                } else {
                    return prve
                }
            }, 0)
            return NewState
        case "CHANGE_CHECK":
            NewState.carList[action.index].flag = !NewState.carList[action.index].flag
            // 对数量进行计算
            NewState.allCount = NewState.carList.reduce((prve, next) => {
                // 如果是选中状态在相加
                if (next.flag) {
                    return prve + next.count
                } else {
                    return prve
                }
            }, 0)
            // 对总价进行计算
            NewState.allPrice = NewState.carList.reduce((prve, next) => {
                if (next.flag) {
                    return prve + next.count * next.price
                } else {
                    return prve
                }
            }, 0)
            return NewState
        case "ALL_CHECK":
            NewState.carList.forEach(item => item.flag = action.flag)
            // 对数量进行计算
            NewState.allCount = NewState.carList.reduce((prve, next) => {
                // 如果是选中状态在相加
                if (next.flag) {
                    return prve + next.count
                } else {
                    return prve
                }
            }, 0)
            // 对总价进行计算
            NewState.allPrice = NewState.carList.reduce((prve, next) => {
                if (next.flag) {
                    return prve + next.count * next.price
                } else {
                    return prve
                }
            }, 0)
            return NewState
        default:
            return state
    }
}

let initState = {
    initList: [],
    carList: [],
    allCount: 0,
    allPrice: 0
}

let store = createStore(reducer, initState, applyMiddleware(thunk))

export default store
